package com.jiantong.lawEnforcing.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.lawEnforcing.dto.SimplenessCasesDto;
import com.jiantong.lawEnforcing.impl.dao.CasesBasicDao;
import com.jiantong.lawEnforcing.impl.dao.PartiesInformationDao;
import com.jiantong.lawEnforcing.impl.dao.SimplenessCasesDao;
import com.jiantong.lawEnforcing.model.CasesBasic;
import com.jiantong.lawEnforcing.model.PartiesInformation;
import com.jiantong.lawEnforcing.model.SimplenessCases;
import com.jiantong.lawEnforcing.service.SimplenessCasesService;
import com.jiantong.lawEnforcing.util.LawUtil;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 当场处罚决定书
 * Created by mz03 on 2017/8/14.
 */
@Slf4j
@Service
@RpcProvider
public class SimplenessCasesServiceImpl implements SimplenessCasesService {

    private final SimplenessCasesDao simplenessCasesDao;
    private final CasesBasicDao casesBasicDao;
    private final PartiesInformationDao partiesInformationDao;

    @Autowired
    public SimplenessCasesServiceImpl(SimplenessCasesDao simplenessCasesDao, CasesBasicDao casesBasicDao, PartiesInformationDao partiesInformationDao) {
        this.simplenessCasesDao = simplenessCasesDao;
        this.casesBasicDao = casesBasicDao;
        this.partiesInformationDao = partiesInformationDao;
    }

    /**
     * id 查找
     *
     * @param id
     * @return simplenessCases
     */
    @Override
    public Response<SimplenessCases> finById(Long id) {
        try {
            SimplenessCases simplenessCases = simplenessCasesDao.findById(id);
            if (simplenessCases != null) {
                return Response.ok(simplenessCases);
            } else {
                log.error("simplenessCases (id={}) is not found", id);
                return Response.fail("simplenessCases.not.found");
            }
        }catch (Exception e){
            log.error("failed to find simplenessCases(id={}),cause:{}",id, Throwables.getStackTraceAsString(e));
            return Response.fail("simplenessCases.find.fail");
        }
    }

    /**
     * id 查找 DTO
     *
     * @param id
     * @return News
     */
    @Override
    public Response<SimplenessCasesDto> findSimplenessCasesDto(Long id) {
        try{
            SimplenessCases simplenessCases =   finById(id).getResult();
            CasesBasic casesBasic = casesBasicDao.findById(simplenessCases.getCases_basic_id());
            if (casesBasic == null){
                log.error("simplenessCases (id={}) is not found", simplenessCases.getCases_basic_id());
                return Response.fail("simplenessCases.not.found");
            }
            PartiesInformation partiesInformation = partiesInformationDao.findById(casesBasic.getParties_id());
            if (partiesInformation == null){
                log.error("partiesInformation (id={}) is not found", casesBasic.getParties_id());
                return Response.fail("partiesInformation.not.found");
            }
            SimplenessCasesDto simplenessCasesDto = new SimplenessCasesDto();
            simplenessCasesDto.setSimplenessCases(simplenessCases);
            simplenessCasesDto.setCasesBasic(casesBasic);
            simplenessCasesDto.setPartiesInformation(partiesInformation);
            return Response.ok(simplenessCasesDto);
        }catch(Exception e){
            log.error("failed to find simplenessCasesDto(id={}),cause:{}",id, Throwables.getStackTraceAsString(e));
            return Response.fail("simplenessCasesDto.find.fail");
        }
    }

    /**
     * 根据cases_basic_id查询DTO
     * @param cases_basic_id
     * @return
     */
    @Override
    public Response<SimplenessCasesDto> findByCases_basic_id(Long cases_basic_id) {
        try{
            Map<String,Object> map = Maps.newHashMap();
                if (cases_basic_id != null){
                    map.put("cases_basic_id",cases_basic_id);
                }

            List<SimplenessCases> list =  simplenessCasesDao.list(map);
            if (list.size()==0){
                log.error("simplenessCases (cases_basic_id={}) is not found",cases_basic_id);
                return Response.fail("simplenessCases.not.found");
            }

            SimplenessCases simplenessCases = list.get(0);
            CasesBasic casesBasic = casesBasicDao.findById(simplenessCases.getCases_basic_id());
            if (casesBasic == null){
                log.error("simplenessCases (id={}) is not found", simplenessCases.getCases_basic_id());
                return Response.fail("simplenessCases.not.found");
            }

            PartiesInformation partiesInformation = partiesInformationDao.findById(casesBasic.getParties_id());
            if (partiesInformation == null){
                log.error("partiesInformation (id={}) is not found", casesBasic.getParties_id());
                return Response.fail("partiesInformation.not.found");
            }
            SimplenessCasesDto simplenessCasesDto = new SimplenessCasesDto();
            simplenessCasesDto.setSimplenessCases(simplenessCases);
            simplenessCasesDto.setCasesBasic(casesBasic);
            simplenessCasesDto.setPartiesInformation(partiesInformation);
            return Response.ok(simplenessCasesDto);
        }catch(Exception e){
            log.error("failed to find simplenessCasesDto(id={}),cause:{}",cases_basic_id, Throwables.getStackTraceAsString(e));
            return Response.fail("simplenessCasesDto.find.fail");
        }
    }

    /**
     * 创建
     *
     * @param simplenessCases 单据对象
     * @return 主键 id
     */
    @Override
    public Response<Long> create(SimplenessCases simplenessCases) {
        try {
            simplenessCasesDao.create(simplenessCases);
            return Response.ok(simplenessCases.getId());
        }catch (Exception e) {
            log.error("failed.to.create.simplenessCases:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("create.simplenessCases.failed");
        }
    }

    /**
     * 更新
     *
     * @param simplenessCases 单据对象
     * @return true or false
     */
    @Override
    public Response<Boolean> update(SimplenessCases simplenessCases) {
        try{
            return Response.ok(simplenessCasesDao.update(simplenessCases));
        }catch (Exception e){
            log.error("failed.to.update.simplenessCases:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("update.simplenessCases.failed");
        }
    }

    /**
     * 根据状态分页查询
     *
     * @param pageNo
     * @param pageSize
     * @param status
     * @return
     */
    @Override
    public Response<Paging<SimplenessCases>> pagingByStatus(
            Integer pageNo,
            Integer pageSize,
            Integer status,
            String cause_action,
            Integer cases_source,
            Integer parties_type,
            String parties_name
    ) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            Map<String, Object> criteria = Maps.newHashMap();
            if(status != null){
                criteria.put("status",status);
            }
            if(cause_action != null){
                criteria.put("cause_action",cause_action);
            }
            if(cases_source != null){
                criteria.put("cases_source",cases_source);
            }
            if(parties_type != null){
                criteria.put("parties_type",parties_type);
            }
            if(parties_name != null){
                criteria.put("parties_name",parties_name);
            }
            return Response.ok(simplenessCasesDao.paging(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        }catch (Exception e){
            log.error("failed to find simplenessCases(status={}), cause:{}", status, Throwables.getStackTraceAsString(e));
            return Response.fail("simplenessCases.find.fail");
        }
    }

    /**
     * 根据状态分页查询
     * @param pageNo
     * @param pageSize
     * @param status
     * @param cause_action
     * @param cases_source
     * @param parties_type
     * @param parties_name
     * @return
     */
    @Override
    public Response<Paging<SimplenessCasesDto>> pagingSimplenessCasesDto(
            Integer pageNo, Integer pageSize, Integer status,
            String cause_action, Integer cases_source, Integer parties_type, String parties_name
    ) {
        try{
            Paging<SimplenessCases> paging = pagingByStatus(pageNo,pageSize,status,cause_action,cases_source,parties_type,parties_name).getResult();
            List<SimplenessCases> list = paging.getData();
            List<SimplenessCasesDto> simplenessCasesDtos = new ArrayList<>();
            for (SimplenessCases simplenessCases:list) {
                CasesBasic casesBasic = casesBasicDao.findById(simplenessCases.getCases_basic_id());
                PartiesInformation partiesInformation = partiesInformationDao.findById(casesBasic.getParties_id());
                SimplenessCasesDto simplenessCasesDto = new SimplenessCasesDto();
                simplenessCasesDto.setPartiesInformation(partiesInformation);
                simplenessCasesDto.setCasesBasic(casesBasic);
                simplenessCasesDto.setSimplenessCases(simplenessCases);
                simplenessCasesDtos.add(simplenessCasesDto);
            }
            return Response.ok(new Paging<>(paging.getTotal(),simplenessCasesDtos));

        }catch (Exception e){
            log.error("failed to find simplenessCasesDto(status={}), cause:{}", status, Throwables.getStackTraceAsString(e));
            return Response.fail("simplenessCasesDto.find.fail");
        }
    }
}
